using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SaveLoadManager : SingletonMonoBehaviour<SaveLoadManager>
{
    public SavedData _savedData;//ScriptableObject

    private void Awake()
    {
        INI_Singleton();
        DontDestroyOnLoad(this);
        _savedData.Load();
    }

    void Start()
    {
        _savedData.Initialize();
        Reg_CPs();
    }

    void Update()
    {

    }

    private void OnApplicationQuit()
    {
        _savedData.Initialize();
    }

    private void OnDisable()
    {
        _savedData._isINIed = true;
    }

    private void Reg_CPs()
    {
        CheckPoint[] CPs = GetComponentsInChildren<CheckPoint>();
        for (int i = 0; i < CPs.Length; i++)
        {
            CPs[i].Key = i;
            _savedData.Reg_CPs(i, CPs[i]);
        }
    }

    public void Save(int key)
    {
        _savedData.Save(key);
    }

    public void Load()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void GameComplete()
    {
        _savedData.Initialize();
        gameObject.SetActive(false);
    }
}
